import React, {useState, useRef} from 'react';
import {message, Button, Tabs, Divider,DatePicker,Popconfirm} from 'antd';
import {PageHeaderWrapper} from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';
import {connect} from 'umi';
import {queryUnitAssessList,saveAndUpdatePerformanceUnitRecord,deletePerformanceUnitRecordById} from '../../service';
import {assessGradeList} from "@/utils/constant";
import UnitRecordFrom from '../../components/UnitRecordFrom';
import moment from 'moment';

const {TabPane} = Tabs;

const TableList = () => {
  const actionRef = useRef();
  const actionRef2 = useRef();
  const [type,setType] = useState(null);
  const [tabIndex,setTabIndex] = useState('1');
  const [visible, setVisible] = useState(false);
  const [loading, setLoading] = useState(false);
  const [record,setRecord] = useState(null);

  const columns = [
    {
      title: '绩效方案名称',
      dataIndex: 'performanceName',
      align:'center',
      hideInSearch:true
    },
    {
      title: '考核年度',
      dataIndex: 'assessmentYear',
      align:'center',
      render: (text) => {
        return text != '-' ? moment(text).format('YYYY') + '年' : '';
      },
      renderFormItem: (item, props) => {
        return <DatePicker className='widthStyle' picker="year" {...props}/>;
      }
    },
    {
      title: '考核公司',
      dataIndex: 'orgName',
      align:'center',
      hideInSearch:true
    },
    {
      title: '考核周期',
      dataIndex: 'assessmentCycle',
      align:'center',
      render: (text) => {
        return text + '月';
      }
    },
    tabIndex === '1' ? {
        title: '绩效级别',
        dataIndex: 'performanceLevel',
        align: 'center',
        hideInSearch: true,
        render: (text) => {
          return assessGradeList.find((item) => item.value === text)?.label;
        }
      } :
      {
        title: '考核分数',
        dataIndex: 'score',
        align: 'center',
        hideInSearch: true,
        render: (text) => {
          return text + '分';
        }
      },
    {
      title: '申请人',
      align:'center',
      dataIndex: 'name',
    },
    {
      title: '申请人工号',
      align:'center',
      hideInSearch: true,
      dataIndex: 'code',
    },
    tabIndex === '2' ? {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      align:'center',
      render: (item, record) => (
        <>
          <a onClick={() => open('edit',record)}>修改</a>
          <Divider type="vertical"/>
          <Popconfirm title={`是否确认删除?`} onConfirm={() => delManageRecord(record.id)} okText="是" cancelText="否">
            <a>删除</a>
          </Popconfirm>
        </>
      ),
    } : {},
  ];

  const delManageRecord = (id) => {
    deletePerformanceUnitRecordById(id).then(res => {
      if (res.flag) {
        message.success('记录删除成功！');
        actionRef2.current && actionRef2.current.reload();
      } else {
        message.error(res.error);
      }
    })
  };

  const handleChangTabIndex = (key) => {
    setTabIndex(key);
    if (key === '1') {
      actionRef.current && actionRef.current.reload();
    } else {
      actionRef2.current && actionRef2.current.reload();
    }
  };


  const open = (type,record) =>{
    if(record){
      setRecord(record);
    }
    setType(type);
    setVisible(true);
  };

  const handleCancel = () => {
    setVisible(false);
    setRecord(null);
    setType(null);
  };

  const handleSubmit = (values) => {
    setLoading(true)
    saveAndUpdatePerformanceUnitRecord(values).then((res) => {
      if (res.flag) {
        handleCancel();
        message.success('操作成功！');
        actionRef.current && actionRef.current.reload();
      } else {
        message.error(res.error);
      }
      setLoading(false);
    })
  };



  return (
    <PageHeaderWrapper>
      <Tabs animated={false} defaultActiveKey="1" onChange={handleChangTabIndex}>
        <TabPane tab="季度" key="1">
          <ProTable
            actionRef={actionRef}
            rowKey="id"
            request={(params) => queryUnitAssessList(params, 1)}
            columns={columns}
          />
        </TabPane>
        <TabPane tab="月度" key="2">
          <ProTable
            actionRef={actionRef2}
            rowKey="id"
            request={(params) => queryUnitAssessList(params, 2)}
            columns={columns}
            toolBarRender={() => [
              <Button key={0} onClick={() => open('import',null)}>导入</Button>,
              <Button key={1} type="primary" onClick={() => open('add',null)}>新建</Button>,
            ]}
          />
          <UnitRecordFrom
            loading={loading}
            type={type}
            record={record}
            visible={visible}
            onCancel={handleCancel}
            onSubmit={handleSubmit}
          />
        </TabPane>
      </Tabs>
    </PageHeaderWrapper>
  )
};

export default connect(({}) => ({}))(TableList)
